package com.soft.mainsite.module.staticstrategy.service;

import cn.hutool.core.util.ObjectUtil;
import com.soft.mainsite.module.staticstrategy.dao.TemplateManageDao;
import com.soft.mainsite.module.staticstrategy.dao.TemplateRelationDao;
import com.soft.mainsite.module.staticstrategy.entity.StaticStrategy;
import com.soft.mainsite.module.staticstrategy.entity.TemplateManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>此类用于模板管理</p>
 * <p>@author:xuzhenjia</p>
 * <p>@date:2019/4/23</p>
 * <p>@remark</p>
 */
@Service
public class TemplateManagerServiceImpl implements TemplateManagerService {
	@Autowired
	private TemplateManageDao templateManageDao;

	@Autowired
	private TemplateRelationDao templateRelationDao;

	@Autowired
	private StaticTriggerService staticTriggerService;

	@Autowired
	private TemplateRelationService templateRelationService;



	@Override
	public TemplateManage querfyTemplateManageByName(String templateName) {
		return templateManageDao.querfyTemplateManage(null, templateName);
	}

	@Override
	public List<TemplateManage> queryCommonTemplateManage(int storagePathType) {
		return templateManageDao.queryCommonTemplateManage(storagePathType);
	}

    @Override
    public List<TemplateManage> querySingleTemplateManage(int staticType) {
        return templateManageDao.querySingleTemplateManage(staticType);
    }



	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
	@Override
	public void saveTemplateMange(StaticStrategy staticStrategy) {

		if (ObjectUtil.isNotNull(staticStrategy.getTemplateId())) {
			templateManageDao.modifyTemplateManage(staticStrategy.getTemplateId(), staticStrategy.getTemplateName(), staticStrategy.getStoragePathType()
					, staticStrategy.getStaticType(), staticStrategy.getRenderTemplateName(), staticStrategy.getBasePath());
		} else {
			templateManageDao.addTemplateManage(staticStrategy.getTemplateName(), staticStrategy.getStoragePathType()
					, staticStrategy.getStaticType(),  staticStrategy.getRenderTemplateName(), staticStrategy.getBasePath());
		}

		templateRelationService.saveOrUpdateTemplateRelation(staticStrategy);
		staticTriggerService.saveOrUpdateTemplateTrigger(staticStrategy);

	}



	/**
	 * 根据模板名称查询关联静态化的模板
	 * @param templateName
	 * @return
	 */
	@Override
	public List<TemplateManage> queryRelationManageByParnetTemplate(String templateName) {
		return templateManageDao.queryRelationManageByParnetTemplate(templateName);
	}


	/**
	 * 根据模板名称查询触发静态化的模板
	 * @param templateName
	 * @return
	 */
	@Override
	public List<TemplateManage> queryTriggerManageByParnetTemplate(String templateName) {
		return templateManageDao.queryTriggerManageByParnetTemplate(templateName);
	}

	@Override
	public List<TemplateManage> queryCommonSingleTemplateManage(Integer singleStatic) {
		return templateManageDao.getCommonSingleTemplateManage(singleStatic);
	}
}
